<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      v-model="form"
      :data="data"
      :page.sync="page"
      :search.sync="query"
      :before-open="beforeOpen"
      ref="crud"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @row-del="rowDel"
      @selection-change="selectionChange"
      @search-change="searchChange"
      @search-reset="searchReset"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template slot="menuLeft">
        <!--        <el-button type="success" size="small" icon="el-icon-download" @click="handleExport">导出</el-button>-->
      </template>
      <template slot-scope="{ row }" slot="menu">
        <el-button type="text" size="small" icon="el-icon-view" @click="handleView(row, 'view')">详情</el-button>
        <el-button size="small" type="text" v-if="permission.odqcInspectionRecords_reportForms" @click="viewQcExamineReport(row)">检验报表</el-button>
        <el-button size="small" type="text" v-if="permission.odqcInspectionRecords_report" @click="viewApaasExamineReport(row)">检验报告</el-button>
      </template>
      <template slot="menuRight">
        <user-table-config :option="option" @confirm="(val) => handleSetUserConfig(val, pageCode)"></user-table-config>
      </template>
    </avue-crud>
    <!--检验报告-->
    <el-dialog
      v-if="apaasInspectionReportDialog"
      fullscreen
      append-to-body
      :modal-append-to-body="true"
      :close-on-click-modal="false"
      title="检验报告"
      :visible.sync="apaasInspectionReportDialog"
    >
      <view-apaas-inspection-report :operateBtn="false" v-if="apaasInspectionReportDialog" ref="apaasReport"></view-apaas-inspection-report>
    </el-dialog>
    <view-over-time-wait-inspect ref="handleInspect" :visible.sync="handleVisable" @closeDialog="handleCloseBeforeInspect"></view-over-time-wait-inspect>
    <!--公用模态弹框-->
  </basic-container>
</template>
<script>
import { mapGetters } from 'vuex';
import userTableConfig from '@/components/user-table-config/index.vue';
import userConfig from '@/mixins/userConfig';
import viewOverTimeWaitInspect from '@/views/quality/viewOverTimeWaitInspect';
import { filterEmptyValues } from '@/util/util';
import {
  getList,
  add,
  getWorkOrderDetail,
  getLineList,
  getDetail,
  getMainTypeList,
  getSubTypeList,
  revoke,
  handle,
  secondTimeInspection,
  getODQCList
} from '@/api/quality/firstInspectionRecord';
import viewApaasInspectionReport from '@/views/designer/preview';
import exportExcel from '@/components/export-excel';
export default {
  name: 'overTimeRecord',
  components: {
    userTableConfig,
    exportExcel,
    viewOverTimeWaitInspect,
    viewApaasInspectionReport
  },
  mixins: [userConfig()],
  created() {
    let queryParams = this.$route.query;
    if (queryParams && queryParams.partNo) {
      this.query.partNo = decodeURIComponent(queryParams.partNo);
    }
  },
  data() {
    return {
      handleVisable: false,
      form: {},
      query: {},
      tableOption: {},
      modalName: '',
      loading: false,
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      data: [],
      apaasInspectionReportDialog: false,
      option: {
        height: 'calc(100vh - 408px)',
        calcHeight: 30,
        selectionList: [],
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        border: true,
        index: true,
        viewBtn: false,
        editBtn: false,
        delBtn: false,
        addBtn: false,
        indexLabel: '序号',
        columnBtn: false,
        selection: true,
        menuWidth: 220,
        column: [
          {
            label: '检验单号',
            prop: 'documentNo',
            searchOrder: 5,
            search: true
          },
          {
            label: '物料编码',
            prop: 'partNo',
            search: true
          },
          {
            label: '物料名称',
            prop: 'partName',
            search: true
          },
          {
            label: '物料型号',
            prop: 'partModel'
          },
          {
            label: '物料批次',
            prop: 'lotNo'
          },
          {
            label: '送检数量',
            prop: 'sendExamineQuantity'
          },
          {
            label: '抽检数量',
            prop: 'examineQuantity'
          },
          {
            label: '检验方式',
            prop: 'examineMode',
            type: 'select',
            dicUrl: '/api/blade-system/dict/dictionary?code=inspection_mode',
            props: {
              label: 'dictValue',
              value: 'dictKey'
            }
          },
          {
            label: '检验结果',
            prop: 'examineResult'
          },
          {
            label: '不合格数量',
            prop: 'ngQuantity'
          },
          {
            label: '不合格率',
            prop: 'ngRate'
          },
          {
            label: '检验人员',
            prop: 'examineByName'
          },
          {
            label: '不良处理人',
            prop: 'disposeByName'
          },
          {
            label: '检验时间',
            prop: 'sendExamineDate'
          }
        ]
      },

      abnormalForm: {},
      abnormalOption: {
        submitBtn: false,
        emptyBtn: false,
        labelWidth: 150,
        column: [
          {
            label: '不良代码',
            prop: 'failureCode',
            type: 'search',
            appendClick: () => {
              this.openFailureCommonModal();
            },
            rules: [
              {
                required: true,
                message: '请选择不良代码',
                trigger: 'blur'
              }
            ],
            disabled: true
          },
          {
            label: '不良原因（大类）',
            prop: 'mainType',
            type: 'select',
            dicData: [],
            rules: [
              {
                required: true,
                message: '请选择不良原因（大类）',
                trigger: 'blur'
              }
            ],
            change: (val) => {
              if (val.value) {
                this.getSubTypeList(val.value);
              } else {
                const column = this.findObject(this.abnormalOption.column, 'subType');
                column.dicData = [];
                this.abnormalForm.subName = '';
              }
            }
          },
          {
            label: '不良原因（小类）',
            prop: 'subType',
            type: 'select',
            multiple: true,
            dicData: []
          },
          {
            label: '不良描述',
            prop: 'faultDescription',
            type: 'textarea',
            span: 24,
            maxRows: 3,
            minRows: 3
          },
          {
            label: '改善措施',
            prop: 'improvementMeasure',
            type: 'textarea',
            span: 24,
            maxRows: 3,
            minRows: 3,
            rules: [
              {
                required: true,
                message: '请选择改善措施',
                trigger: 'blur'
              }
            ]
          },
          {
            label: '备注',
            prop: 'remark',
            type: 'textarea',
            span: 24,
            maxRows: 3,
            minRows: 3
          },
          {
            label: '附件',
            prop: 'attachments',
            type: 'upload',
            span: 16,
            loadText: '图片上传中，请稍等',
            propsHttp: {
              url: 'link',
              name: 'attachId',
              res: 'data'
            },
            listType: 'picture-card',
            accept: 'image/png, image/jpeg, image/jpg',
            tip: '只能上传png/jpeg/jpg文件',
            action: '/api/blade-resource/oss/endpoint/put-file-attach'
          }
        ]
      },
      otherInfoForm: {},
      otherInfoOption: {
        submitBtn: false,
        emptyBtn: false,
        labelWidth: 150,
        column: [
          {
            label: '检验时间',
            prop: 'examineDate',
            span: 8,
            disabled: true
          },
          {
            label: '检验人员',
            prop: 'examineByName',
            span: 8,
            disabled: true
          },
          {
            label: '最新编辑人',
            prop: 'updateUserName',
            span: 8,
            disabled: true
          },
          {
            label: '最新编辑时间',
            prop: 'updateTime',
            span: 8,
            disabled: true
          }
        ]
      },
      lineList: [],
      documentNo: '',
      attachmentsList: [],
      inspectionPictureList: [],
      pictureList: [],
      abnormalRecordIndex: 0,
      mainTypeList: [],
      subTypeList: [],
      openHandleDialogType: 'edit',
      exprotVisible: false,
      btnLoading: false,
      exportData: []
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.$structuralSplit();
    });
    if (this.$route.query.partNo) {
      this.$set(this.query, 'partNo', this.$route.query.partNo);
      this.onLoad(this.page);
    }
  },
  computed: {
    ...mapGetters(['permission'])
  },
  methods: {
    async viewQcExamineReport(row) {
      window.open(`${this.website.reportUrl}/preview?_u=blade-超期检验报表模板.ureport.xml&qcId=${row.id}`);
    },
    viewApaasExamineReport(row) {
      if (!row.examineContent) {
        this.$message({
          type: 'warning',
          message: '检验报告信息不存在!'
        });
        return;
      }
      let examineContent = JSON.parse(row.examineContent || '{}');
      this.apaasInspectionReportDialog = true;
      this.$nextTick(() => {
        this.$refs.apaasReport.setDataJson(examineContent);
      });
    },
    rowSave(row, done, loading) {},
    rowUpdate(row, index, done, loading) {},
    rowDel(row) {},
    beforeOpen(done, type) {},
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      params.descs = 'update_time';
      getODQCList(page.currentPage, page.pageSize, Object.assign(params, this.query, { documentType: 'ODQC' })).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    },
    handleView(row, type) {
      this.$refs.handleInspect.getData(row.id);
      this.handleVisable = true;
    },
    handleCloseBeforeInspect() {
      this.onLoad(this.page);
    }
  }
};
</script>
<style lang="scss" scoped>
::v-deep .firstInspectionRecordDialog {
  .el-dialog__body {
    padding-top: 10px;
    .titleIcon {
      display: flex;
      align-items: center;
      margin: 5px 0;
      &.flex {
        display: flex;
        justify-content: space-between;
        align-items: center;
      }
      .title {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 0;
      }
      .name {
        font-weight: bold;
      }
    }
  }
}
</style>
